package graph.drawings;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class VertexDrawing extends AbtractDrawing {
	// private Vertex vertex;
	public Point2D.Double position;

	public VertexDrawing(String name, Point position) {
		// this.vertex = new Vertex();
		this.key = name;
		this.position = new Point2D.Double();
		this.position.x = position.x;
		this.position.y = position.y;

		bound = new Bound();
		updateBoundByPosition();
	}
	
	public VertexDrawing(String name, Point2D.Double position) {
		// this.vertex = new Vertex();
		this.key = name;
		this.position = new Point2D.Double();
		this.position.x = position.x;
		this.position.y = position.y;

		bound = new Bound();
		updateBoundByPosition();
	}

	//
	// public VertexDrawing(Vertex vertex, Point position) {
	// this.vertex = vertex;
	// this.position = position;
	// bound = new Bound(0, 0, 0, 0);
	// updateBoundByPosition();
	//
	// this.key = vertex.getKey();
	// }

	public Point2D getPosition() {
		return this.position;
	}

	public void updatePosition(Point point) {
		this.position.x = point.x;
		this.position.y = point.y;

		updateBoundByPosition();
	}

	@Override
	public void draw(Graphics2D graphics) {

		// draw circle, Java isn't support drawing circle direct
		// so we use the method drawOval and calculator the position of center
		// point
		graphics.setColor(Setting.VertexBoundaryColor);
		Ellipse2D.Double circle = new Ellipse2D.Double(this.position.x
				- Setting.VertexSize, this.position.y - Setting.VertexSize,
				Setting.VertexSize * 2, Setting.VertexSize * 2);
		graphics.draw(circle);
		graphics.setColor(Setting.VertexBackgroundColor);
		graphics.fill(circle);

		// draw text, calculator position of text by font
		int widthByKey = this.key.length();
		Font font = Setting.VertexFont;
		graphics.setFont(Setting.VertexFont);
		graphics.setColor(Setting.vertexForgroundColor);
		graphics.drawString(this.key,
				(float)(this.position.x - widthByKey * font.getSize() / 3),
				(float)(this.position.y + font.getSize() / 2 - 2));

		if (selected)
			drawHandles(graphics);

	}

	public void move(final Point from, final Point to) {
		this.position.x = this.position.x + to.x - from.x;
		this.position.y = this.position.y + to.y - from.y;

		updateBoundByPosition();
	}

	@Override
	public void drawHandles(Graphics2D graphics) {
		graphics.setColor(Color.GRAY);
		drawHandle(graphics, bound.x1, bound.y1);
		drawHandle(graphics, bound.x2, bound.y1);
		drawHandle(graphics, bound.x1, bound.y2);
		drawHandle(graphics, bound.x2, bound.y2);
	}

	@Override
	public boolean testHit(Point point) {
		return bound.ContainsPoint(point.x, point.y);
	}

	private void updateBoundByPosition() {
		bound.setBound(position.x - Setting.VertexSize, position.y
				- Setting.VertexSize, position.x + Setting.VertexSize,
				bound.y2 = position.y + Setting.VertexSize );
	}
}
